<?php

namespace App\Admin\Controllers;


use App\AdminRole;
use App\AdminUser;

class UserController extends Controller
{

    //列表页面
    public function index()
    {
        $users = AdminUser::paginate(10);
//        $users = AdminUser::pagenate(10)->get();
        return view('admin/user/index', compact('users'));
    }

    //添加页面
    public function create()
    {
        return view('admin/user/create');
    }

    //添加行为
    public function store()
    {
        $this->validate(request(), [
            'name'  => 'required|min:2',
            'password'=>'required|min:6'
        ]);

        $name = request('name');
        $password = bcrypt(request('password'));

        AdminUser::create(compact('name', 'password'));

        return redirect('/admin/users');
    }

    //当前用户有哪些角色
    public function role(AdminUser $user)
    {
        $roles   = AdminRole::all();
        $myRoles = $user->roles;

        return view('admin/user/role', compact('roles', 'myRoles', 'user'));
    }

    //保存用户的角色
    public function storeRole(AdminUser $user)
    {
        $this->validate(request(), [
           'roles' => 'required|array'
        ]);

        $roles = AdminRole::findMany(request('roles'));
        $myRoles = $user->roles;

        //要增加的角色
        $addRoles = $roles->diff($myRoles);
        foreach ($addRoles as $role) {
            $user->assignRole($role);
        }

        //要删除的角色
        $delRoles = $myRoles->diff($roles);
        foreach ($delRoles as $role) {
            $user->deleteRole($role);
        }

        return back();
    }

//    //用户设置页面
//    public function setting()
//    {
//        return view('user.setting');
//    }
//
//    //保存用户设置
//    public function settingStore()
//    {
//        return null;
//    }
//
//    //显示用户信息
//    public function show(User $user)
//    {
//        //这个人的信息, 含 关注、粉丝、文章数
//        $user = User::withCount(['fans','stars', 'posts'])->find($user->id);
//
//        //这个人的文章列表, 取创建时间最新的 10 条
//        $posts = $user->posts()->orderBy('created_at', 'desc')->take(10)->get();
//
//        //这个人关注的用户, 包含关注用户的 关注、粉丝、文章数
//        $stars = $user->stars;
//        $susers = User::whereIn('id', $stars->pluck('star_id'))->withCount(['fans','stars', 'posts'])->get();
//
//        //这个人的粉丝 关注、粉丝、文章数
//        $fans = $user->fans;
//        $fusers =  User::whereIn('id', $fans->pluck('fan_id'))->withCount(['fans','stars', 'posts'])->get();
//
//        return view('user/show', compact('user', 'posts', 'susers', 'fusers'));
//    }
//
//    //关注某个用户
//    public function fan(User $user)
//    {
//        $me = \Auth::user();
//        $res = $me->doFan($user->id);
//        dd($res);
//
//        return [
//            'error' => 0,
//            'msg'   => ''
//        ];
//    }
//
//    //取消关注某个用户
//    public function unfan(User $user)
//    {
//        $me = \Auth::user();
//        $me->doUnfan($user->id);
//
//        return [
//            'error' => 0,
//            'msg'   => ''
//        ];
//    }
}
